# TurboWiki is a simple wiki module for TurboGears
#
# Copyright (C) 2009 Alberto Granzotto <agranzot@gmail.com>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy<
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# 
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

'''The module contains the widgets to use with the wiki.

If you want to visualise a page you have to import 'article_widget' from this
module and pass to it a **kw of values (specified under the Article class).

'''
import turbogears as tg
from turbogears.widgets import Widget, CSSLink
import utils
import forms

class Article(Widget):
    '''This class visualise the commands, the article and the form (if needed).

    Commands are the typical actions you can do with a wiki:
      * view;
      * edit;
      * view the history.

    This class is used for the 'view' and the 'edit' actions.
    In the 'view' case you need to call the display method in this way:
    ${article_widget.display(
        wiki_name='name',
        wiki_title='The Title',
        wiki_content='The <em>HTMLized</em> content')}

    In the 'edit' case you need to call the display method in this way:
    ${article_widget.display(
        wiki_name='name',
        wiki_title='The Title',
        wiki_content='The <em>HTMLized</em> content',
        wiki_edit_form=turbowiki.forms.wiki_form,      #or anything you like
        wiki_edit_submit='save',
        wiki_edit_action='../do_edit',
        wiki_edit_values=dict(
            title='The Title',
            lang='en',
            text='The //wikiformatted// code')
        )}

    To avoid to call article_widget with different parameters, WikiController
    returns a dict called wiki_values that contains the three values specified
    above if the method is 'view' or the seven values specified above if the
    method is 'edit'.
    In this way you need only to put in your view template this line:
     
     ${article_widget.display(**wiki_values)}
    '''
    template = """
    <div xmlns:py="http://purl.org/kid/ns#"
        class="wiki_widget">
        <div py:if="show_commands" class="wiki_commands">
            <ul>
                <li><a href="$wiki_base_url/view/$wiki_name">
                    Page</a></li>
                <li><a href="$wiki_base_url/edit/$wiki_name">
                    Edit</a></li>
                <li><a href="$wiki_base_url/history/$wiki_name">
                    History</a></li>
            </ul>
        </div>
        <div class="wiki_title">
            <h1>$wiki_title</h1>
            <p py:if="defined('wiki_revision')">
                revision <span class="wiki_revision">$wiki_revision</span>
                <span class="wiki_created">($wiki_created)</span>
                by <span class="wiki_user">
                <a href="$wiki_base_url/view/usr.$wiki_user">$wiki_user</a></span>
            </p>
        </div>
        <div py:if="defined('wiki_edit_form')">
            ${wiki_edit_form.display(
                submit_text=wiki_edit_submit,
                action=wiki_edit_action,
                value=wiki_edit_values)}
        </div>
        <div py:if="wiki_content" class="wiki_content">
            ${XML(wiki_content)}
        </div>
    </div>
    """
    params = ['wiki_base_url', 'show_commands']
    wiki_base_url = utils.wiki_base_url
    show_commands = tg.config.get('turbowiki.article.show_commands', True)
    css = [CSSLink('turbowiki.static.css', 'style.css')]
article_widget = Article()

class History(Widget):
    '''This class visualise the commands and the history of changes.

    Commands are the typical actions you can do with a wiki:
      * view;
      * edit;
      * view the history.

    To visualise a history you need to call the display method in this way:
     ${history_widget.display(
         wiki_name='name',
         wiki_title='The Title',
         wiki_history=[content1, content2...],
     )}
    '''
    template = """
    <div xmlns:py="http://purl.org/kid/ns#"
        class="wiki_widget">
        <div class="wiki_commands">
            <ul>
                <li><a href="$wiki_base_url/view/$wiki_name">
                    Page</a></li>
                <li><a href="$wiki_base_url/edit/$wiki_name">
                    Edit</a></li>
                <li><a href="$wiki_base_url/history/$wiki_name">
                    History</a></li>
            </ul>
        </div>
        <h1>$wiki_title</h1>
        <table class="wiki_history">
            <tr>
                <th>Revision</th>
                <th>Language</th>
                <th>User</th>
                <th>Title</th>
            </tr>
            <tr py:for="h in wiki_history">
                <td>$h.rev
                    <span class="revision_date">$h.pretty_created</span></td>
                <td>$h.lang</td>
                <td><a href="$wiki_base_url/view/usr.$h.user">$h.user</a></td>
                <td><a href="$wiki_base_url/view/$wiki_name/$h.rev">
                    $h.title</a></td>
            </tr>
        </table>
    </div>
    """
    params = ['wiki_base_url']
    wiki_base_url = utils.wiki_base_url
    css = [CSSLink('turbowiki.static.css', 'style.css')]
history_widget = History()
